package com.atguigu.jxc.service.impl;


import com.atguigu.jxc.dao.SupplierDao;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private LogService logService;
    @Autowired
    private SupplierDao supplierDao;

    //    @Override
//    public ServiceVO saveRole(Role role, HttpSession session) {
//        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
//        Role roleDB = roleDao.getRoleByRoleIdUserId(role.getRoleId(), user.getUserId());
//        // 将用户选择的角色信息放入缓存，以便以后加载权限的时候使用
//        session.setAttribute("currentRole", roleDB);
//        System.out.println("db the role --->" + roleDB);
//        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
//    }
//
//    @Override
//    public Map<String, Object> listAll() {
//        Map<String,Object> map = new HashMap<>();
//
//        List<Role> roleList = roleDao.findAll();
//
//        logService.save(new Log(Log.SELECT_ACTION, "查询所有角色信息"));
//
//        map.put("rows", roleList);
//
//        return map;
//    }
//
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();

        int total = supplierDao.getSupplierCount(supplierName);
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> suppliers = supplierDao.getSupplierList(offSet, rows, supplierName);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询角色信息"));
        map.put("total", total);
        map.put("rows", suppliers);
        return map;
    }

    @Override
    public ServiceVO save(Supplier supplier) {
        // supplier为空时，说明是新增操作，需要先判断角色名是否存在
        if (supplier.getSupplierId() == null) {

            Supplier exsupplier = supplierDao.findSupplierByName(supplier.getSupplierName());

            if (exsupplier != null) {

                return new ServiceVO<>(ErrorCode.ROLE_EXIST_CODE, ErrorCode.ROLE_EXIST_MESS);

            }

            logService.save(new Log(Log.INSERT_ACTION, "新增供应商:" + supplier.getSupplierName()));

            supplierDao.insertSupplier(supplier);

        } else {

            logService.save(new Log(Log.UPDATE_ACTION, "修改供应商:" + supplier.getSupplierName()));

            supplierDao.updateSupplier(supplier);

        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);

    }

    @Override
    public ServiceVO delete(String ids) {
        String[] arr = ids.split(",");
        for (int i = 0; i < arr.length;i++) {
            int id=Integer.parseInt(arr[i]);
            logService.save(new Log(Log.DELETE_ACTION, "删除供应商:" +
                    supplierDao.getSupplierById(id).getSupplierName()));
            supplierDao.deleteSupplier(id);

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
//
//    @Override
//    public ServiceVO setMenu(Integer roleId, String menus) {
//        // 先删除当前角色的所有菜单
//        roleMenuDao.deleteRoleMenuByRoleId(roleId);
//
//        // 再赋予当前角色新的菜单
//        String[] menuArray = menus.split(",");
//
//        for(String str : menuArray){
//
//            RoleMenu rm = new RoleMenu();
//
//            rm.setRoleId(roleId);
//
//            rm.setMenuId(Integer.parseInt(str));
//
//            roleMenuDao.save(rm);
//
//        }
//
//        logService.save(new Log(Log.UPDATE_ACTION,"设置"+roleDao.getRoleById(roleId).getRoleName()+"角色的菜单权限"));
//
//        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
//    }
}
